import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, CanActivate, CanLoad, Route, Router, RouterStateSnapshot, UrlSegment } from '@angular/router';
import { NzMessageService } from 'ng-zorro-antd/message';
import { Observable } from 'rxjs';

@Injectable()
export class CanAuthProvide implements CanActivate, CanLoad {

  constructor(
    private msg: NzMessageService
  ) {}

  check(): Observable<boolean> {
    return new Observable((observer) => {
      if (localStorage.getItem('Token')) {
        observer.next(true);
        observer.complete();
        return;
      }

      this.msg.error('请登录');
      observer.next(false);
      observer.complete();
    });
  }

  canActivate(
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot): boolean | Observable<boolean> | Promise<boolean> {
    return this.check();
  }

  canLoad(route: Route): boolean | Observable<boolean> | Promise<boolean> {
    return this.check();
  }

}

